﻿using Sidvall.Serialization;

namespace Sidvall.IdentityManager.Data.Keys
{
    public class LoginIndex
    {
        public string LoginProvider { get; set; }
        public string ProviderKey { get; set; }
        
        public static LoginIndex GetKey(IAspNetUserLogins item)
        {
            if (item == null)
                return null;
            if (item.LoginProvider == null)
                return null;
            if (item.ProviderKey == null)
                return null;
            return new LoginIndex()
            {
                LoginProvider = item.LoginProvider,
                ProviderKey = item.ProviderKey,
            };
        }
        public static LoginIndex GetKey(string loginProvider, string providerKey)
        {
            if (loginProvider == null)
                return null;
            if (providerKey == null)
                return null;
            return new LoginIndex()
            {
                LoginProvider = loginProvider,
                ProviderKey = providerKey,
            };
        }
        public static LoginIndex ParseKey(string key)
        {
            var values = key.Split("|*.");
            return new LoginIndex()
            {
                LoginProvider = values[0],
                ProviderKey = values[1],
            };
        }
        public override string ToString()
        {
            return this.LoginProvider + "|*." + this.ProviderKey;
        }
    }
}